import java.awt.Point;


public class Move {
	
	private Point position;
	private Point velocity;
	public int steps;
	
	/**
	 * Constructor
	 * @param position
	 * @param velocity
	 * @param steps
	 */
	public Move(Point position,Point velocity, int steps) {
		this.position = position;
		this.velocity = velocity;
		this.steps = steps;
	}
	
	/**
	 * Add all adjacent nodes
	 */
	public void addAdjacent() {
		checkAdjacentNode(new Point(-1,-1));
		checkAdjacentNode(new Point(-1, 0));
		checkAdjacentNode(new Point(-1, 1));
		checkAdjacentNode(new Point( 0,-1));
		checkAdjacentNode(new Point( 0, 0));
		checkAdjacentNode(new Point( 0, 1));
		checkAdjacentNode(new Point( 1,-1));
		checkAdjacentNode(new Point( 1, 0));
		checkAdjacentNode(new Point( 1, 1));
	}

	/**
	 * check AdjacentNode
	 * @param velocityChange
	 */
	private void checkAdjacentNode(Point velocityChange) {		
		int x = position.x+velocity.x+velocityChange.x;
		int y = position.y+velocity.y+velocityChange.y;
		int dx = velocity.x+velocityChange.x;
		int dy = velocity.y+velocityChange.y;
		
		Character c = adjacent(velocityChange);
		
		if (c != null) {
			if (c.equals(' ')) {
				Move newNode = new Move(new Point(x,y),new Point(dx,dy),steps+1);
				MarioGraph.addNode(newNode);
			}
			
			if (c.equals('F')) {
				Move newNode = new Move(new Point(x,y),new Point(dx,dy),steps+1);
				MarioGraph.addWinnerNode(newNode);
			}
			
		}
		
	}	
	/**
	 * Find adjacent character
	 * @param velocityChange
	 * @return
	 */
	private Character adjacent(Point velocityChange) {
		int y = position.y+velocity.y+velocityChange.y;
		int x = position.x+velocity.x+velocityChange.x;
		
		if (y < 0) return null;
		if (x < 0) return null;
		if (y >= Level.rows  ) return null;
		if (x >= Level.colums) return null;
		
		return Level.grid[y][x];
	}
	
	/**
	 * Checks whether or not the nodes are the same. (Without looking at the steps)
	 * @param n
	 * @return
	 */
	public boolean isTheSame(Move n) {
		if (n == this) return true;
		return ((n.position.x == position.x) && (n.position.y == position.y) &&
				(n.velocity.x == velocity.x) && (n.velocity.y == velocity.y));
	}
	
	/**
	 * To string
	 */
	@Override
	public String toString() {
		return "Node["+position.x+","+position.y+"]("+velocity.x+","+velocity.y+"):"+steps;
	}
	
}
